using System;
using System.Collections.Generic;
using System.Text;

namespace ZYCommon
{
   
        public class NameValueList
{
    // Fields
    private int iItemCount = 0;
    private string[] strNames = new string[0x10];
    private string[] strValues = new string[0x10];

    // Methods
    public void Clear()
    {
        this.iItemCount = 0;
    }

    public bool Contains(string strName)
    {
        for (int i = 0; i < this.iItemCount; i++)
        {
            if (this.strNames[i] == null)
            {
                break;
            }
            if (this.strNames[i].Equals(strName))
            {
                return true;
            }
        }
        return false;
    }

    public string FixVariableString(string strText)
    {
        return this.FixVariableString(strText, "[", "]");
    }

    public string FixVariableString(string strText, string strHead, string strEnd)
    {
        if ((((strText == null) || (strHead == null)) || ((strEnd == null) || (strHead.Length == 0))) || ((strEnd.Length == 0) || (strText.Length == 0)))
        {
            return strText;
        }
        int index = strText.IndexOf(strHead);
        if (index < 0)
        {
            return strText;
        }
        int startIndex = 0;
        StringBuilder builder = new StringBuilder();
        do
        {
            int num2 = strText.IndexOf(strEnd, (int) (index + 1));
            if (num2 <= index)
            {
                break;
            }
            int num4 = index;
            do
            {
                index = num4;
                num4 = strText.IndexOf(strHead, (int) (num4 + 1));
            }
            while ((num4 > index) && (num4 < num2));
            string strName = strText.Substring(index + strHead.Length, (num2 - index) - strHead.Length);
            int num5 = this.IndexOf(strName);
            if (num5 >= 0)
            {
                if (startIndex < index)
                {
                    builder.Append(strText.Substring(startIndex, index - startIndex));
                }
                builder.Append(this.strValues[num5]);
                index = num2 + strEnd.Length;
                startIndex = index;
            }
            else
            {
                index = num2 + strEnd.Length;
            }
        }
        while ((index >= 0) && (index < strText.Length));
        if (startIndex < strText.Length)
        {
            builder.Append(strText.Substring(startIndex));
        }
        return builder.ToString();
    }

    public int FromStringArray(string[] strData)
    {
        if (((strData != null) && (strData.Length > 0)) && ((strData.Length % 2) == 0))
        {
            this.iItemCount = strData.Length / 2;
            this.strNames = new string[this.iItemCount];
            this.strValues = new string[this.iItemCount];
            for (int i = 0; i < this.iItemCount; i++)
            {
                this.strNames[i] = (strData[i * 2] == null) ? "" : strData[i * 2].Trim();
                this.strValues[i] = strData[(i * 2) + 1];
            }
            return this.iItemCount;
        }
        return -1;
    }

    public string GetName(int index)
    {
        if ((index >= 0) && (index < this.iItemCount))
        {
            return this.strNames[index];
        }
        return null;
    }

    public string GetValue(int index)
    {
        if ((index >= 0) && (index < this.iItemCount))
        {
            return this.strValues[index];
        }
        return null;
    }

    public string GetValue(string strName)
    {
        return this.GetValue(this.IndexOf(strName));
    }

    public int IndexOf(string strName)
    {
        if (strName != null)
        {
            for (int i = 0; i < this.iItemCount; i++)
            {
                if (this.strNames[i] == null)
                {
                    break;
                }
                if (strName.Equals(this.strNames[i]))
                {
                    return i;
                }
            }
        }
        return -1;
    }

    public void Remove(string strName)
    {
        int index = this.IndexOf(strName);
        if (index >= 0)
        {
            for (int i = index; i < this.iItemCount; i++)
            {
                this.strNames[i] = this.strNames[i + 1];
                this.strValues[i] = this.strValues[i + 1];
            }
            this.strNames[this.iItemCount - 1] = null;
            this.strValues[this.iItemCount - 1] = null;
            this.iItemCount--;
        }
    }

    public void SetValue(string strName, string strValue)
    {
        if (strName != null)
        {
            int index = this.IndexOf(strName);
            if (index >= 0)
            {
                this.strValues[index] = strValue;
            }
            else
            {
                if (this.iItemCount >= (this.strNames.Length - 1))
                {
                    string[] strArray = new string[(int) (this.strNames.Length * 1.5)];
                    string[] strArray2 = new string[strArray.Length];
                    for (int i = 0; i < this.iItemCount; i++)
                    {
                        strArray[i] = this.strNames[i];
                        strArray2[i] = this.strValues[i];
                    }
                    this.strNames = strArray;
                    this.strValues = strArray2;
                }
                this.strNames[this.iItemCount] = strName;
                this.strValues[this.iItemCount] = strValue;
                this.iItemCount++;
            }
        }
    }

    public string ToListString(string strItemSpliter, string strValueSpliter)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < this.iItemCount; i++)
        {
            if (i == 0)
            {
                builder.Append(this.strNames[i] + strValueSpliter + this.strValues[i]);
            }
            else
            {
                builder.Append(strItemSpliter + this.strNames[i] + strValueSpliter + this.strValues[i]);
            }
        }
        return builder.ToString();
    }

    public override string ToString()
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("NameValueList(" + this.iItemCount.ToString() + "):");
        for (int i = 0; i < this.iItemCount; i++)
        {
            builder.Append(this.strNames[i] + "=" + this.strValues[i] + " ");
        }
        return builder.ToString();
    }

    public string[] ToStringArray()
    {
        string[] strArray = new string[this.iItemCount * 2];
        for (int i = 0; i < this.iItemCount; i++)
        {
            strArray[i * 2] = this.strNames[i];
            strArray[(i * 2) + 1] = this.strValues[i];
        }
        return strArray;
    }

    // Properties
    public int Count
    {
        get
        {
            return this.iItemCount;
        }
    }

    public string this[int index]
    {
        get
        {
            return this.strValues[index];
        }
    }

    public string this[string strName]
    {
        get
        {
            return this.GetValue(strName);
        }
    }
}

 

 

  
}
